<?php

namespace app\index\controller;

use app\common\lib\common\Redis;
use app\common\lib\common\StringHandle;
use think\Controller;
use think\Request;
use think\Url;

class Index extends Controller
{
    public function index()
    {
        //        每次重启检查redis是否存在以前fd数据
        $fd = Redis::sMembers(StringHandle::live());
        if (!empty($fd)) {
            Redis::del(StringHandle::live());
        }
        return '';
    }

    public function exits()
    {
        go(function () {
            $time = time();
            //        创建http请求
            $reslut = [];
            $address = [
                '0x7b5598c659FfDa9ccC0DD05dac5678deDf088B7a',
                '0xa838be6E4b760E6061D4732D6B9F11Bf578f9A76',
                '0x7EC4d225481F8bada87552e59C0a9DBFc5B10b2f'
            ];
            foreach ($address as $v) {
                //            创建http请求
                $cli = new \Swoole\Coroutine\Http\Client('node3.web3api.com', 80);
                $cli->set(['timeout' => 3]);//设置请求时间
                $cli->setHeaders(['Content-Type' => "application/json",]);
                $cli->setDefer();//延迟收包
                $data = array(
                    'jsonrpc' => '2.0',
                    'method' => 'eth_getBalance',
                    'params' => array($v, 'latest'),
                    'id' => 1
                );
                $cli->post('/', json_encode($data));
                $reslut[$v] = $cli;
//              var_dump($cli->body);
            }
            foreach ($address as $v) {
                $reslut[$v]->recv();//接收包
                // 获取返回文本内容
                $res = json_decode($reslut[$v]->body, true);
                $priceEth = hexdec(str_replace("0x", "", $res["result"]));
                $money = $priceEth / pow(10, 18);
                $db = new \swoole_mysql();
                $server = array(
                    'host' => '120.78.67.1',
                    'port' => 3306,
                    'user' => 'root',
                    'password' => 'liuqiang',
                    'database' => 'swoole',
                    'charset' => 'utf8', //指定字符集
                    'timeout' => 2,  // 可选：连接超时时间（非查询超时时间），默认为SW_MYSQL_CONNECT_TIMEOUT（1.0）
                );
                $db->connect($server, function (\swoole_mysql $db, $r) use ($v, $money) {
                    if ($r === false) {
                        echo '链接失败';
                        var_dump($db->connect_errno, $db->connect_error);
                        return;
                    }
                    $sql = "INSERT INTO eth (path, money)VALUES ('$v','$money')";
//                    print_r($sql);
                    $db->query($sql, function (\swoole_mysql $db, $r) {
                        if ($r === false) {
                            var_dump($db->error, $db->errno);
                        } elseif ($r === true) {
                            var_dump($db->affected_rows, $db->insert_id);
                        }
//                        var_dump($r);
                        $db->close();
                    });
                });
            }
        });
        header("Location: http://www.baidu.com");
        return;
    }
    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
}
